<template>
<div class="vip-code-list views-container">
    <el-dialog @close="saveDialogForm" :visible.sync="dialogTableVisible" custom-class="reset-pwd wlm-form-dialog dialog-table">
        <div class="wlm-form" v-if="dialogTableVisible">
            <div class="wlm-form-header wlm-dialog-fixheader">
                <span class="wlm-dialog-inlenr">生成激活码</span>
            </div>
            <el-scrollbar wrap-class="scrollbar-wrapper">
                <div class="wlm-form-content">
                    <div class="wlm-dialog-main">
                        <div class="wlm-dialog-content">
                            <el-form ref="user" :model="formFormatData.formData" :rules="formFormatData.rules" size="small" label-width="120px" class="retail-form" label-position="right">
                                <el-form-item label="会员卡名称">
                                    <el-select filterable v-model="formFormatData.formData.card_id" placeholder="请选择">
                                        <el-option v-for="item in categoryData" :key="item.card_id" :label="item.card_title" :value="item.card_id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="卡号生产方式">
                                    <el-radio-group v-model="formFormatData.formData.method">
                                        <el-radio :label="1">随机生成</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                                <el-form-item label="激活码个数">
                                    <el-input v-model="formFormatData.formData.number">
                                        <template slot="append">个</template>
                                    </el-input>
                                </el-form-item>
                            </el-form>
                        </div>
                    </div>
                </div>
            </el-scrollbar>
        </div>
        <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
            <el-button size="small" @click="refreshSave" type="primary">保存</el-button>
        </footer>
    </el-dialog>
    <div class="wlm-table">
        <div class="wlm-table-hearder-btn">
            <el-button size="small" @click="dialogTableVisible = true;initChooseUser.closable = true;" type="primary">生成激活码</el-button>
        </div>
        <div class="wlm-table-header">
            <el-form :model="tableFormatData.userTable.files" size="small" label-width="95px" class="retail-form" label-position="right">
                <el-form-item label="" class="filter">
                    <el-form-item label="会员卡号：">
                        <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.userTable.files.card_number" placeholder="请输入会员卡号" style="width:160px;"></el-input>
                    </el-form-item>
                    <el-form-item label="激活码：">
                        <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.userTable.files.cdkey_number" placeholder="请输入激活码" style="width:160px;"></el-input>
                    </el-form-item>
                    <el-form-item label="绑定状态：">
                        <el-select v-model="tableFormatData.userTable.files.cdkey_is_use" placeholder="请选择" style="width:200px;">
                            <el-option label="全部" :value="0">
                            </el-option>
                            <el-option label="已绑定" :value="1">
                            </el-option>
                            <el-option label="未绑定" :value="2">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="" class="filter-btns">
                    <el-button type="primary" @click="filesSerch">筛选</el-button>
                    <el-button plain icon="document" :data-query="JSON.stringify(tableFormatData.userTable.files)" v-exceldown="{path:'exportCdKey&',query: {}}">{{$t('excel.export') }}</el-button>
                    <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="wlm-table-content">
            <el-table :ref="tableFormatData.userTable.key" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange" style="width: 100%">
                <!-- <el-table-column type="selection" width="55">
                </el-table-column> -->
                <el-table-column prop="date" label="会员卡号" >
                    <template slot-scope="scope">
                        <span>{{scope.row.card_number || '-'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="会员卡名称" >
                    <template slot-scope="scope">
                        <p>{{scope.row.card_title}}</p>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间"  min-width="90">
                    <template slot-scope="scope">
                        <span>{{scope.row.create_time}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="绑定状态" >
                    <template slot-scope="scope">
                        <span v-if="scope.row.cdkey_is_use=='1'">已绑定</span>
                        <span v-if="scope.row.cdkey_is_use=='2'">未绑定</span>
                    </template>
                </el-table-column>
                <el-table-column label="绑定人" >
                    <template slot-scope="scope">
                        <div class="wlm-table-logos">
                            <img v-if="scope.row.avatarUrl" class="logo" :src="scope.row.avatarUrl">
                            <div class="inner-text">
                                <p>{{scope.row.nickName || '-'}}</p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="绑定时间"  min-width="90">
                    <template slot-scope="scope">
                        <span v-if="!!scope.row.bind_time">{{scope.row.bind_time | parseTime('{y}-{m}-{d}')}}</span>
                        <span v-if="!scope.row.bind_time">-</span>
                    </template>
                </el-table-column>
                <el-table-column label="激活码"  min-width="90">
                    <template slot-scope="scope">
                        <span>{{scope.row.cdkey_number}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" >
                    <template slot-scope="scope">
                        <div class="operation-group">
                            <el-button v-if="!scope.row.nickName&&!scope.row.can_delete" class="wlm-text" type="text" @click="delTableItem(scope.row.cdkey_id)">删除</el-button>
                            <span v-if="!!scope.row.nickName||!!scope.row.can_delete">-</span>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-content flex-row flex-justify-e flex-align-c">
                <!-- <el-row class="pagination-btns">
                    <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.userTable.files.checkall" @change="toggleSelection">全部</el-checkbox>
                    <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
                </el-row> -->
                <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import {
  getCdKeyInfo,
  destoryCdKey,
  selectedCard,
  createdCdKey
} from '@/api/application'
import mixins from '@/mixins/mixins'
import exceldown from '@/directive/exceldown'
export default {
  mixins: [mixins.getters('Table'), mixins.getters('Form')],
  name: 'VipCodeList',
  components: {
  },
  directives: {
    exceldown
  },
  created() {
    this.$nextTick(() => {
      selectedCard().then((response) => {
        const {
          data: {
            data: msgData = []
          }
        } = response
        this.categoryData = msgData
      })
    })
  },
  data() {
    return {
      dialogTableVisible: false,
      selecStoreData: [],
      categoryData: [],
      blogTitle: '',
      initChooseUser: {
        closable: true,
        initList: []
      },
      formFormatData: {
        key: 'user',
        api: {
          editForm: {
            api: getCdKeyInfo,
            params: {
              id: ''
            }
          },
          submitForm: createdCdKey
        },
        formData: {
          card_id: '',
          method: 1,
          number: ''
        },
        rules: {
          password: [{
            required: true,
            message: '请输入核销密码',
            trigger: 'blur'
          }]
        }
      },
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: getCdKeyInfo,
            delList: destoryCdKey
          },
          tableData: [],
          files: {
            cdkey_number: '',
            card_number: '',
            cdkey_is_use: '',
            ids: [],
            checkall: '0',
            Recycle: '1'
          },
          change: {
            status: {
              key: 'status',
              api: getCdKeyInfo
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    saveDialogForm() {
      this.initChooseUser.initList = []
      this.resetDialogFormData({ card_id: '', method: 1, number: '' })
    },
    refreshSave() {
      this.formFormatSubmit()
      this.dialogTableVisible = false
      this.filesSerch()
    }
  }
}
</script>
